package com.sitech.ibnms.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 *
 * @author liujhc
 */
public class KpiValueMap {

    public static class Key {

        private String unitId;
        private String kpiId;

        public Key(String unitId, String kpiId) {
            this.unitId = unitId;
            this.kpiId = kpiId;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof Key) {
                Key k = (Key) obj;
                return k.unitId != null && k.kpiId != null && k.unitId.equals(unitId) && k.kpiId.equals(kpiId);
            }
            return false;
        }

        @Override
        public int hashCode() {
            return ("{unitId:'" + unitId + "',kpiId:'" + kpiId + "'}").hashCode();
        }

        public String getKpiId() {
            return kpiId;
        }

        public String getUnitId() {
            return unitId;
        }
    }
    private Map<Key, String> map = new HashMap();

    public KpiValueMap() {
    }

    public static KpiValueMap parse(String unitId, String kpiId, String kpiValue) {
        KpiValueMap kvMap = new KpiValueMap();
        kvMap.put(unitId, kpiId, kpiValue);
        try {
            JSONArray json = JSONArray.fromObject(kpiValue);
            for (int i = 0; i < json.size(); i++) {
                JSONObject elem = json.getJSONObject(i);
                kvMap.put(elem.getString("unitId"), elem.getString("kpiId"), elem.getString("kpiValue"));
            }
        } catch (Exception e) {
            //解析出现异常，表示是单指标
            //e.printStackTrace(System.out);
        }
        return kvMap;
    }

    public void put(String unitId, String kpiId, String kpiValue) {
        map.put(new Key(unitId, kpiId), kpiValue);
    }

    public String get(String unitId, String kpiId) {
        return map.get(new Key(unitId, kpiId));
    }

    @Override
    public String toString() {
        List<Key> keys = new ArrayList(map.keySet());
        JSONArray json = JSONArray.fromObject(keys);
        for (int i = 0; i < keys.size(); i++) {
            json.getJSONObject(i).put("kpiValue", map.get(keys.get(i)));
        }
        return json.toString();
    }

    /**
     * 测试代码
     * @param args 
     */
    public static void main(String[] args) {
        //创建对象
        KpiValueMap kvMap1 = new KpiValueMap();
        kvMap1.put("u1", "k1", "v1");
        kvMap1.put("u2", "k2", "v2");
        kvMap1.put("u3", "k3", "v3");
        //获取kpiValue
        System.out.println("kvMap1.get(\"u1\",\"k1\") = " + kvMap1.get("u1", "k1"));
        System.out.println("kvMap1.get(\"u2\",\"k2\") = " + kvMap1.get("u2", "k2"));
        System.out.println("kvMap1.get(\"u3\",\"k3\") = " + kvMap1.get("u3", "k3"));
        //将对象转换为字符串
        System.out.println("kvMap1 = " + kvMap1.toString());

        //解析字符串成对象
        KpiValueMap kvMap2 = KpiValueMap.parse("u", "k", kvMap1.toString());
        //查看解析的准确性
        System.out.println("kvMap2 = " + kvMap2);
        System.out.println("kvMap2.get(\"u1\",\"k1\") = " + kvMap2.get("u1", "k1"));
        System.out.println("kvMap2.get(\"u2\",\"k2\") = " + kvMap2.get("u2", "k2"));
        System.out.println("kvMap2.get(\"u3\",\"k3\") = " + kvMap2.get("u3", "k3"));

        //单指标
        KpiValueMap kvMap3 = KpiValueMap.parse("u", "k", "v");
        System.out.println("kvMap3.get(\"u\",\"k\") = " + kvMap3.get("u", "k"));

    }
}
